package com.api.oa.config;

import java.util.HashMap;
import java.util.Map;

public class UserContext {

    private final static ThreadLocal<Map<String, Object>> holder = new ThreadLocal<>();

    public static void remove() {
        holder.remove();
    }

    public static void set(String key, Object value) {
        getContext().put(key, value);
    }

    public static Object get(String key) {
        return getContext().get(key);
    }

    private static  Map<String, Object> getContext() {
        Map map = holder.get();
        if (map == null) {
            map = new HashMap();
            holder.set(map);
        }
        return map;
    }


    /**
     * 获取用户的iD
     * @return
     */
    public static Long getUserId(){
        return (Long) get("userId");
    }
    /**
     * 获取用户角色
     * @return
     */
    public static Long getRole(){
        return (Long) get("role");
    }

    public static String getInsCode(){
        return get("insCode") == null ? null : (String)get("insCode");
    }
}
